<?php
namespace Clover\Controller;

use Think\Controller;

class NewsController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        session('menu', "news");
    }

    /**
     * 新闻列表 显示分类id=1的 新闻列表
     * @param int cat_id 默认值1
     */
    public function index()
    {
        check_privilege();
        session('menu_sub', "news_list");
        $login_status = D("Admin")->get_login_status();
        if (empty($login_status)) {
            $this->redirect('/Clover/login');
        }
        $cat_id = I("get.cat_id", 0);
        $news_cat_list = D("News")->get_news_cat_list();
        $news_list = D("News")->get_news_list($cat_id);

        $data = array(
            "news_cat_list" => $news_cat_list,
            "news_list" => $news_list,
            "cat_id" => $cat_id
        );
        $this->assign($data);
        $this->display("news_list");
    }

    /**
     * 跳转到添加页面
     * @return [type] [description]
     */
    public function add_news()
    {
        check_privilege();
        $news_id = I("get.id", 0);
        $news_cat_list = D("News")->get_news_cat_list();
        $data = array(
            "news_id" => $news_id,
            "news_cat_list" => $news_cat_list,
        );
        $this->assign($data);
        $this->display("news_add");
    }

    /**
     * 执行新闻添加操作
     */
    public function do_add()
    {
        $title = I("post.title", "");
        $cat_id = I("post.cat_id", 0);
        $content = I("post.content", "", false);
        $News = M("News");
        $data = array(
            "title" => $title,
            "cat_id" => $cat_id,
            "content" => $content,
            "add_time" => time()
        );
        $status = $News->data($data)->add();
        if ($status) {
            echo json_encode(array("status" => $status));
        } else {
            echo json_encode(array("status" => -1));
        }
    }

    /**
     * 跳转到修改页面
     * @return [type] [description]
     */
    public function edit_news()
    {
        check_privilege();
        $news_id = I("get.id", 0);
        $news_cat_list = D("News")->get_news_cat_list();
        $news_info = M("News")->where("id=" . $news_id)->find();
        $data = array(
            "news_id" => $news_id,
            "news_cat_list" => $news_cat_list,
            "news_info" => $news_info,
            "add_time" => time()
        );
        $this->assign($data);
        $this->display("news_edit");
    }

    /**
     * 执行新闻修改操作
     */
    public function do_edit()
    {
        $id = I("post.id", 0);
        $title = I("post.title", "");
        $cat_id = I("post.cat_id", 0);
        $content = I("post.content", "", false);
        $News = M("News");
        $data = array(
            "title" => $title,
            "cat_id" => $cat_id,
            "content" => $content
        );
        $res = $News->where("id=" . $id)->save($data);
        echo $res;
    }

    /**
     * 执行新闻删除
     * @return [type] [description]
     */
    public function do_delete()
    {
        $id = I("post.id", 0);
        $News = M("News");
        $res = $News->where("id=$id")->delete();
        echo $res;
    }

    /**
     * 新闻分类列表页面
     *
     */
    public function category_list()
    {
        check_privilege();
        session('menu_sub', "news_cat");
        $News = M("News");
        $category_list = $News->table("dsk_news_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_list");
    }

    /**
     * 跳转到分类信息添加页面
     */
    public function add_category()
    {
        check_privilege();
        $News = M("News");
        $category_list = $News->table("dsk_news_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_add");
    }

    public function do_add_category()
    {
        $parent_id = I("post.parent_id", 0);
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $News = D("News");
        $data = array(
            "parent_id" => $parent_id,
            "cat_name" => $cat_name,
            "sort_order" => $sort_order,
            "add_time" => time()
        );
        echo $News->getLastSql();
        $status = $News->table("dsk_news_category")->data($data)->add();
        echo $status;
    }

    /**
     * 跳转到分类信息修改页面
     */
    public function edit_category()
    {
        check_privilege();
        $id = I("get.id", 0);
        $News = D("News");
        $category_list = $News->table("dsk_news_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level", 1, array('id' => $id));
        $category_info = $News->table("dsk_news_category")->where("id=" . $id)->find();
        $data = array(
            "category_list" => $result,
            "category_info" => $category_info,
            "cat_id" => $id
        );
        $this->assign($data);
        $this->display("category_edit");
    }

    public function do_edit_category()
    {
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $id = I("post.id", 0);

        $News = D("News");
        $data = array(
            "cat_name" => $cat_name,
            "sort_order" => $sort_order
        );
        $res = $News->table("dsk_news_category")->where("id=" . $id)->save($data);
        echo $res;
    }

    /**
     * 执行新闻分类删除同时删除旗下子集
     * @return [type] [description]
     */
    public function do_delete_category()
    {
        $cat_id = I("post.id", 0);
        $del_ids = $cat_id;
        $childrenIds = D("News")->get_Children_Ids($cat_id);
        if ($childrenIds) {
            $del_ids .= $childrenIds;
        }
        $News = M("News");
        $map['id'] = array('in', $del_ids);
        $status = $News->table("dsk_news_category")->where($map)->delete();
        echo $status;
    }
}